package com.db.williamchart.plugin;

import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import com.db.williamchart.Grid;
import com.db.williamchart.data.Frame;
import com.db.williamchart.data.Label;

import java.util.Arrays;
import java.util.List;

public class AxisGrid implements Grid {

    private final float DEFAULT_STROKE_WIDTH = 5f;
    private final float DEFAULT_DASH_INTERVAL = 10f;

    private GridType gridType = GridType.FULL;
    private GridEffect gridEffect = GridEffect.SOLID;
    private int color = -0x1000000;
    private float strokeWidth = DEFAULT_STROKE_WIDTH;

    public GridType getGridType() {
        return gridType;
    }

    public void setGridType(GridType gridType) {
        this.gridType = gridType;
    }

    public GridEffect getGridEffect() {
        return gridEffect;
    }

    public void setGridEffect(GridEffect gridEffect) {
        this.gridEffect = gridEffect;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public float getStrokeWidth() {
        return strokeWidth;
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    private Paint paint;

    @Override
    public void draw(Canvas canvas, Frame innerFrame, List<Float> xLabelsPositions, List<Float> yLabelsPositions) {
        if (paint == null) {
            initPaint();
        }

        if (gridType == GridType.FULL || gridType == GridType.VERTICAL) {
            for (Float item : xLabelsPositions) {
                canvas.drawLine(item, innerFrame.getBottom(), item, innerFrame.getTop(), paint);
            }
        }

        if (gridType == GridType.FULL || gridType == GridType.HORIZONTAL) {
            for (Float item : yLabelsPositions) {
                canvas.drawLine(innerFrame.getLeft(), item, innerFrame.getRight(), item, paint);
            }
        }
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(strokeWidth);
        if (gridEffect != GridEffect.SOLID) {
            float interval = DEFAULT_STROKE_WIDTH;
            if (gridEffect == GridEffect.DASHED) {
                interval = DEFAULT_STROKE_WIDTH * 2;
            }
            paint.setPathEffect(
                    new DashPathEffect(
                            new float[]{interval, interval},
                            0f
                    )
            );
        }
    }
}
